package com.eml.service.dao.impl;

import com.eml.service.dao.UserDao;
import com.eml.service.entity.*;
import com.eml.service.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl implements UserDao {
    /**
     * 用户登录(查询用户是否存在)
     */
    @Override
    public User userLogin(String userName) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql =
        "select user_id,user_name,user_password,user_age,user_gender,user_phone from user " +
                "where user_name= ? ";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, userName);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            int userId = resultSet.getInt("user_id");
            String name = resultSet.getString("user_name");
            String password = resultSet.getString("user_password");
            int age = resultSet.getInt("user_age");
            boolean userGender = resultSet.getBoolean("user_gender");
            String userPhone = resultSet.getString("user_phone");

            JDBCUtils.close(connection,preparedStatement,resultSet);

            return new User(userId,name,password,age,userGender,userPhone);
        }
        JDBCUtils.close(connection,preparedStatement,resultSet);
        return null;
    }
    /**
     * 用户注册
     * */
    @Override
    public void userRegister(User user) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql =
        "insert into user(user_id,user_name,user_password,user_age,user_gender,user_phone) " +
                "values(default,?,?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,user.getUserName());
        preparedStatement.setString(2, user.getUserPassword());
        preparedStatement.setInt(3,user.getAge());
        preparedStatement.setBoolean(4,user.isUserGender());
        preparedStatement.setString(5,user.getUserPhone());
        preparedStatement.executeUpdate();
        JDBCUtils.close(connection,preparedStatement);
    }
    /**
     * 修改密码
     * */
    @Override
    public int updateUserPassword(String userName,String userPassword) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql =
        "update user set user_password = ? where user_name = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, userPassword);
        preparedStatement.setString(2, userName);
        return preparedStatement.executeUpdate();
    }
    /**
     * 查询个人信息
     * */
    @Override
    public User selectUserInfo(String username,String userPassword) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql =
        "select user_id,user_name,user_password,user_age,user_gender,user_phone from user " +
                "where user_name=? and user_password=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, username);
        preparedStatement.setString(2,userPassword);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            int userId = resultSet.getInt("user_id");
            String name = resultSet.getString("user_name");
            String password = resultSet.getString("user_password");
            int age = resultSet.getInt("user_age");
            boolean userGender = resultSet.getBoolean("user_gender");
            String userPhone = resultSet.getString("user_phone");
            JDBCUtils.close(connection,preparedStatement,resultSet);
            return new User(userId,name,password,age,userGender,userPhone);
        }
        JDBCUtils.close(connection,preparedStatement,resultSet);
        return null;
    }
    /**
     * 查看商家信息
     * */
    @Override
    public Merchant selectMerchantInfo(String merchantName) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql =
        "select merchant_id,merchant_phone,merchant_name from merchant " +
         "where merchant_name = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,merchantName);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            int id = resultSet.getInt("merchant_id");
            String phone = resultSet.getString("merchant_phone");
            String name = resultSet.getString("merchant_name");
            JDBCUtils.close(connection,preparedStatement,resultSet);
            return new Merchant(id,null,null,phone,name);
        }
        JDBCUtils.close(connection,preparedStatement,resultSet);
        return null;
    }
    /**
     * 查看菜单
     * */
    @Override
    public Menu selectMenuInfo(Integer menuId) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql =
        "select menu_quantity,menu_note,menu_remain,menu_name from menu where menu_id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,menuId);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            int menuQuantity = resultSet.getInt("menu_quantity");
            String menuNote = resultSet.getString("menu_note");
            int menuRemain = resultSet.getInt("menu_remain");
            String name = resultSet.getString("menu_name");
            JDBCUtils.close(connection,preparedStatement,resultSet);
            return new Menu(menuQuantity,menuNote,menuRemain,name);
        }
        JDBCUtils.close(connection,preparedStatement,resultSet);
        return null;
    }
    /**
     * 更改订单状态
     */
    @Override
    public int commitOrderStatus(boolean orderDetailsStatus,Integer orderDetailsId) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql =
        "update orderdetail set orderdetails_status = ? where orderdetails_id =?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setBoolean(1,orderDetailsStatus);
        preparedStatement.setInt(2,orderDetailsId);
        return preparedStatement.executeUpdate();
    }
    /**
     * 查看订单状态
     * */
    @Override
    public Orderdetail selectOrderStatus(Integer orderDetailsId) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql =
        "select orderdetails_status from orderfetil where orderdetails_id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,orderDetailsId);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            int id = resultSet.getInt("orderdetails_id");
            boolean status = resultSet.getBoolean("orderdetails_status");
            JDBCUtils.close(connection,preparedStatement,resultSet);
            return new Orderdetail(id,null,null,null,status);
        }
        JDBCUtils.close(connection,preparedStatement,resultSet);
        return null;
    }
    /**
     * 查看支付信息
     * */
    @Override
    public Payment selectPayment(Integer payId) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql =
                "select Pay_ways,Pay_success,Pay_amount,Pay_record from payment where Pay_id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,payId);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            String payWays = resultSet.getString("Pay_ways");
            boolean paySuccess = resultSet.getBoolean("Pay_success");
            int payAmount = resultSet.getInt("Pay_amount");
            String payRecord = resultSet.getString("Pay_record");
            JDBCUtils.close(connection,preparedStatement,resultSet);
            return new Payment(payWays,paySuccess,payAmount,payRecord);
        }
        JDBCUtils.close(connection,preparedStatement,resultSet);
        return null;
    }
    /***
     *查看配送员状态
     */
    @Override
    public DeliveryStatus selectDelivererStatus(Integer DeliveryStatusId) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql =
                "select DeliveryStatus_name from orderfetil where DeliveryStatus_id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,DeliveryStatusId);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            int id = resultSet.getInt("DeliveryStatus_id");
            String status = resultSet.getString("DeliveryStatus_name");
            JDBCUtils.close(connection,preparedStatement,resultSet);
            return new DeliveryStatus(id,status);
        }
        JDBCUtils.close(connection,preparedStatement,resultSet);
        return null;
    }
    /**
     * 修改订单状态
     * */
    @Override
    public int updateOrderStatus(boolean orderDetailsStatus,Integer orderDetailsId) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql =
                "update orderdetail set orderdetails_status = ? where orderdetails_id =?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setBoolean(1,orderDetailsStatus);
        preparedStatement.setInt(2,orderDetailsId);
        return preparedStatement.executeUpdate();
    }
    /**
     * 添加评价
     * */
    @Override
    public void commitReview(Review review) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql =
                "insert into review(R_scores,R_connect) " +
                        "values(?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,review.getReviewScores());
        preparedStatement.setString(2,review.getReviewConnect());
        preparedStatement.executeUpdate();
        JDBCUtils.close(connection,preparedStatement);
    }
    /**
     * 添加反馈
     * */
    @Override
    public void commitFeedback(Feedback feedback) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql =
                "insert into feedback (name,type,info) " +
                        "values(?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, feedback.getFeedbackName());
        preparedStatement.setString(2, feedback.getFeedbackType());
        preparedStatement.setString(3, feedback.getFeedbackInfo());
        preparedStatement.executeUpdate();
        JDBCUtils.close(connection,preparedStatement);
    }
}
